package com.ricky.service_hospital.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ricky.hosp.model.hosp.Department;
import com.ricky.hosp.vo.hosp.DepartmentQueryVo;
import com.ricky.hosp.vo.hosp.DepartmentVo;
import com.ricky.service_hospital.repository.DepartmentRepository;
import com.ricky.service_hospital.service.DepartmentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;
    //上传科室接口
    @Override
    public void save(Map<String, Object> paramMap) {
        //paraMap转换成department对象
        String paramMapString = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(paramMapString,Department.class);
        //判断是否已经存在该科室信息，根据医院编号和科室编号进行查询
        Department departmentExist = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
        if (departmentExist!=null){
            departmentExist.setUpdateTime(new Date());
            departmentExist.setIsDeleted(0);
            departmentRepository.save(departmentExist);
        }else{
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        //创建pageable对象，设置当前页和每页记录数
        Pageable pageable = PageRequest.of(page-1,limit);
        //创建Example对象
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        department.setIsDeleted(0);
        ExampleMatcher matcher = ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase(true);
        Example<Department>example = Example.of(department,matcher);
        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;
    }

    @Override
    public void remove(String hoscode, String depcode) {
        //根据医院编号和科室编号查询信息
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department!=null){
            departmentRepository.deleteById(department.getId());
        }
    }

    @Override
    public List<DepartmentVo> findDepartTree(String hoscode) {
        //创建list集合用于最终数据封装
        List<DepartmentVo> result = new ArrayList<>();
        //根据医院编号查询所有科室信息
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);
        Example<Department> example = Example.of(departmentQuery);
        //所有科室列表信息
        List<Department> departmentList = departmentRepository.findAll(example);
        //根据大科室编号 bigcode 分组，获取每个大科室里面下级子科室
        Map<String, List<Department>> departmentMap = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //遍历map集合
        for (Map.Entry<String, List<Department>> entry :
               departmentMap.entrySet() ) {
            //大科室编号
            String bigcode = entry.getKey();
            //大科室对应的全局科室信息
            List<Department> generalDeptInfo = entry.getValue();
            //封装大科室
            DepartmentVo departmentVoBig = new DepartmentVo();
            departmentVoBig.setDepcode(bigcode);
            departmentVoBig.setDepname(generalDeptInfo.get(0).getBigname());
            //封装小科室
            ArrayList<DepartmentVo> subDepartments = new ArrayList<>();
            for (Department department :
                    generalDeptInfo) {
                DepartmentVo subDepartmentVo = new DepartmentVo();
                subDepartmentVo.setDepname(department.getDepname());
                subDepartmentVo.setDepcode(department.getDepcode());
                //封装到list集合
                subDepartments.add(subDepartmentVo);
            }
            //将小科室集合放到大科室中
            departmentVoBig.setChildren(subDepartments);
            result.add(departmentVoBig);
        }
        return result;
    }
    // 根据可是编号，医院编号查询科室名称
    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department != null) {
            return department.getDepname();
        }
        return null;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        return department;
    }
}
